package com.yanchangapp.ui.presenter;

import com.yanchangapp.base.HeadApiRetrofit;
import com.yanchangapp.commom.AppConstant;
import com.yanchangapp.model.request.SubmitApplyRequest;
import com.yanchangapp.model.response.GoodsApplyResponse;
import com.yanchangapp.ui.view.GoodsApplyAtView;
import com.zxl.baselib.bean.response.BaseResponse;
import com.zxl.baselib.ui.base.BaseActivity;
import com.zxl.baselib.ui.base.BaseObserver;
import com.zxl.baselib.ui.base.BasePresenter;
import com.zxl.baselib.ui.base.ExceptionHandle;

import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by apple on 2019-08-21.
 * description: 经营品种p层
 */
public class GoodsApplyAtPresenter extends BasePresenter<GoodsApplyAtView> {

    public GoodsApplyAtPresenter(BaseActivity context) {
        super(context);
    }

    public void quertList(int curPage){
        HeadApiRetrofit.getInstance().queryApplyGoddsList(curPage)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<GoodsApplyResponse>() {
                    @Override
                    public void onNext(GoodsApplyResponse goodsApplyResponse) {
                        if (getView() != null){
                            if (AppConstant.REQUEST_SUCCESS == goodsApplyResponse.getCode()){
                                getView().queryListSuccess(
                                        goodsApplyResponse.getData().getList(),
                                        goodsApplyResponse.getData().getCurrPage() < goodsApplyResponse.getData().getTotalPage());
                            }else {
                                getView().queryListFailure(goodsApplyResponse.getMsg());
                            }
                        }
                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseException exception) {
                        if (getView() != null){
                            getView().queryListFailure(exception.getErrorInfo());
                        }
                    }
                });
    }

    public void submitApplyList(List<SubmitApplyRequest> submitApplyRequests){
        if (null != getView()){
            getView().showLoadingDialog();
        }
        HeadApiRetrofit.getInstance().submitGoodsApply(submitApplyRequests)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<BaseResponse>() {
                    @Override
                    public void onNext(BaseResponse baseResponse) {
                        if (null != getView()){
                            getView().hideLoadingDialog();
                            if (AppConstant.REQUEST_SUCCESS == baseResponse.getCode()){
                                getView().submitApplySuccess();
                            }else {
                                getView().submitApplyFailure(baseResponse.getMsg());
                            }
                        }
                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseException exception) {
                        if (null != getView()){
                            getView().hideLoadingDialog();
                            getView().submitApplyFailure(exception.getErrorInfo());
                        }
                    }
                });
    }
}
